package cn.edu.lnsf.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Scanner;
import java.util.Date;
import java.util.Vector;

import cn.edu.lnsf.dbutils.OrderFactory;
import cn.edu.lnsf.model.Count;
import cn.edu.lnsf.model.Orders;
import cn.edu.lnsf.service.IordersService;
import cn.edu.lnsf.service.IroomService;
import cn.edu.lnsf.service.impl.OrdersServiceImpl;
import cn.edu.lnsf.service.impl.RoomServiceImpl;

public class OrdersController {

	private IordersService service = new OrdersServiceImpl();
	private IroomService roomService = new RoomServiceImpl();
	public Orders o = new Orders();
	public Scanner scanner = new Scanner(System.in);



	// 生成订单
	public void insertOrders(Orders ot) {

		@SuppressWarnings("unused")
		String customerIDCard, roomNumber, remarks, checkOutTime, checkInTime, orderTime;
		int totalMoney;
		long t = OrderFactory.exchangeID(ot.getCustomerIDCard());
		System.out.println();
		System.out.print("\t\t\t\t❤订单号：");
//		System.out.println(OrderFactory.getOrderCode(t));
		String orderNumber = OrderFactory.getOrderCode(t);
		System.out.println(orderNumber);
		String orderStayus = "预定中";
		o.setOrderNumber(orderNumber);
		o.setOrderStatus(orderStayus);
		o.setCustomerIDCard(ot.getCustomerIDCard());
		System.out.println();
		System.out.println("\t\t\t\t\t      ❤☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆房间类型☆☆☆☆☆☆☆☆☆☆☆☆☆☆❤");
		System.out.println("\t\t\t\t\t      ❤                 优品单人房、优品双人房、豪华套房、总统套房       ❤          ");
		System.out.println("\t\t\t\t\t      ❤☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆❤");
		System.out.println();
		System.out.print("\t\t\t\t❤请输入房间类型：");
		String roomType = scanner.nextLine();
		System.out.println();
		roomService.findRoomByroomType(roomType);
		System.out.println();
		System.out.println("\t\t\t\t\t\t\t☆☆☆☆❤    请填写预订表单   ❤☆☆☆☆");
		System.out.println();
		System.out.print("\t\t\t\t\t\t\t\t❤房间号：");
		roomNumber = scanner.next();

		System.out.print("\t\t\t\t\t\t\t\t❤入住时间：");
		checkInTime = scanner.next();
		System.out.print("\t\t\t\t\t\t\t\t❤退房时间：");
		checkOutTime = scanner.next();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date Indate = format.parse(checkInTime);
			o.setCheckInTime(Indate);
			Date Outdate = format.parse(checkOutTime);
			o.setCheckOutTime(Outdate);
			ot.setCheckInTime(Indate);
			ot.setCheckOutTime(Outdate);
		} catch (ParseException e) {
			System.out.println("\t\t\t\t\t\t\t  ✪     不合法的输入    ✪");
			e.printStackTrace();
		}
		System.out.print("\t\t\t\t\t\t\t\t❤总住宿天数为：");
		int day = (int) service.totalAllMoney(ot);
		totalMoney = roomService.findRoomMoneyByroomNumber(roomNumber) * day;
		System.out.print("\t\t\t\t\t\t\t\t❤总金额为：");
		System.out.println(totalMoney);
		System.out.print("\t\t\t\t\t\t\t\t❤服务员ID：");
		String waiterID = scanner.next();
		o.setWaiterID(waiterID);
		System.out.print("\t\t\t\t\t\t\t\t❤备注：");
		remarks = scanner.next();
		o.setRoomNumber(roomNumber);
		o.setTotalMoney(totalMoney);
		
		o.setRemarks(remarks);
		if (service.insertOrders(o) == true) {
//			service.insertOrders(o);
			System.out.println();
			System.out.println("\t\t\t\t\t\t\t  ✪     订单号已经生成..    ✪");
			System.out.println();
			System.out.println("\t\t\t\t\t\t\t❤ ☆☆☆☆   订单信息   ☆☆☆☆❤");
			System.out.println();
			Vector<Orders> v = new Vector<Orders>();
			v = service.findOrdersByID(o);
			
			for(Orders o1 : v){
				System.out.println("\t\t\t\t\t\t\t☆订单号："+o1.getOrderNumber());
				System.out.println("\t\t\t\t\t\t\t☆顾客身份证号："+o1.getCustomerIDCard());
				System.out.println("\t\t\t\t\t\t\t☆房间号码："+o1.getRoomNumber());
				System.out.println("\t\t\t\t\t\t\t☆入住时间："+o1.getCheckInTime());
				System.out.println("\t\t\t\t\t\t\t☆退房时间："+o1.getCheckOutTime());
                System.out.println("\t\t\t\t\t\t\t☆总金额："+o1.getTotalMoney());
		        System.out.println("\t\t\t\t\t\t\t☆服务员ID："+o1.getWaiterID());
				System.out.println("\t\t\t\t\t\t\t☆备注："+o1.getRemarks());
				System.out.println("\t\t\t\t\t\t\t☆预订时间："+o1.getOrderTime());
			}
		} else {
			System.out.println("\t\t\t\t\t\t\t  ✪     订单生成失败，请重新申请...    ✪");
		}
		System.out.println();
		System.out.println("\t\t\t\t\t\t\t\t  ✪       预订成功！     ✪");
	}
	
//	//查看订单
//	public void findOrdersByID(Orders record){
//		System.out.println("1查看订单");
//		System.out.println("2退出");
//		
//		service.findOrdersByID(record);
//		
//	}

	//年报表
	public void coumtYearOrders() {
		System.out.println("\t\t\t\t\t\t\t☆☆☆☆☆☆☆☆☆年报表☆☆☆☆☆☆☆☆☆");
		System.out.println();
		for (Count c : service.countYearOrders()) {			
			System.out.println("\t\t\t\t\t\t\t      年份：" + c.getCountYear() + "  " + "经营额："
					+ c.getCountMoney());
		}
		System.out.println();
		System.out.println("\t\t\t\t\t\t\t☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆");

	}

	//月报表
	public void coumtMonthOrders() {
		System.out.println("\t\t\t\t\t\t\t☆☆☆☆☆☆☆☆☆月报表☆☆☆☆☆☆☆☆☆");
		System.out.println();
		for (Count c : service.countMonthOrders()) {
			System.out.println("\t\t\t\t\t\t\t      月份：" + c.getCountMonth() + "  " + "经营额："
					+ c.getCountMoney());
		}
		System.out.println();
		System.out.println("\t\t\t\t\t\t\t☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆");
	}

	//周报表
	public void coumtWeekOrders() {
		System.out.println("\t\t\t\t\t\t\t☆☆☆☆☆☆☆☆☆周报表☆☆☆☆☆☆☆☆☆");
		System.out.println();
		for (Count c : service.countWeekOrders()) {
			System.out.println("\t\t\t\t\t\t\t      周：" + c.getCountWeek() + "  " + "经营额："
					+ c.getCountMoney());
		}
		System.out.println();
		System.out.println("\t\t\t\t\t\t\t☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆");
	}

	//天报表
		public void coumtDayOrders() {
			OrderTrackingController otk = new OrderTrackingController();
			System.out.println("\t\t\t\t\t\t\t☆☆☆☆☆☆☆☆☆日报表☆☆☆☆☆☆☆☆☆");
			System.out.println();
			for (Count c : service.countDayOrders()) {
				System.out.println("\t\t\t\t\t\t\t      日：" + c.getCountDay() + "  " + "经营额："
						+ c.getCountMoney());
			}
			for(Count  info : otk.countTodayInfo()){
				System.out.println("\t\t\t\t\t\t\t☆         今天入住房间数量："+info.getCountOrders());
			}
			for(Count  outfo : otk.countTodayOutfo()){
				System.out.println("\t\t\t\t\t\t\t☆         今天退房房间数量："+outfo.getCountOrders());
			}
			System.out.println();
			System.out.println("\t\t\t\t\t\t\t☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆");
		}
	
		//订单报表
		public void countOrders(){
			RoomController rCon = new RoomController();
			System.out.println("\t\t\t\t\t\t\t☆☆☆☆☆☆☆☆☆订单报表☆☆☆☆☆☆☆☆☆");
			System.out.println();
				for(Count c : service.countOrders()){
					System.out.println("\t\t\t\t\t\t\t\t订单号数量： "+c.getCountOrders());
				}
				System.out.println();
				System.out.println("\t\t\t\t\t\t\t☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆");
				System.out.println("\t\t\t\t\t\t\t☆   未入住的房间数量    ");
				rCon.coumtRoomEmpty();
				System.out.println("\t\t\t\t\t\t\t☆    已入住的房间数量   ");
				rCon.coumtRoomNotempty();
				
				
			}

	public void findAllOrders() {

		for (Orders os : service.findAllOrders()) {
			System.out.println(os.getOrderNumber());
			System.out.println(os.getOrderStatus() + ","
					+ os.getCustomerIDCard() + "," + os.getRoomNumber() + ","
					+ os.getCheckInTime() + "," + os.getCheckOutTime() + ","
					+ os.getTotalMoney() + "," + os.getWaiterID() + ","
					+ os.getRemarks() + "," + os.getOrderTime());
		}
	}

	// 入住
	public void updateOrderInfo(){
		System.out.print("\t\t\t\t❤请输入需要办理入住的订单号：");
		Orders oi = new Orders();
		String orderNumber = scanner.next();
		
		oi.setOrderNumber(orderNumber);
		if(service.updateOrderInfo(oi)==true){
			System.out.println();
		System.out.println("\t\t\t\t\t\t\t\t  ✪       成功办理入住！    ✪");
		}else{
			System.out.println("\t\t\t\t\t\t\t\t  ✪       订单号不存在....    ✪");
		}
		
	}

	// 退房
	public void updateOrderOutfo(){
		System.out.print("\t\t\t\t❤请输入需要办理退房的订单号：");
		String orderNumber = scanner.next();
		Orders ou = new Orders();
		ou.setOrderNumber(orderNumber);
		service.updateOrderOutfo(ou);
		System.out.println();
		System.out.println("\t\t\t\t\t\t\t     ✪       退房成功！欢迎下次光临！    ✪");
	}
	
	// 删除订单
	public void deleteOrders(String ordersID){
		System.out.print("\t\t\t\t❤请输入需要删除的订单号：");
		ordersID = scanner.next();
		service.deleteOrders(ordersID);
	}
	
	public void finance(){
		int choose = -1;
		while(choose!=0){
			System.out.println();
			System.out.println("\t\t\t\t\t\t\t\t☆      财务报表    ☆");
			System.out.println();
			System.out.println("\t\t\t\t\t\t\t\t   1、天报表");
			System.out.println("\t\t\t\t\t\t\t\t   2、周报表");
			System.out.println("\t\t\t\t\t\t\t\t   3、月报表");
			System.out.println("\t\t\t\t\t\t\t\t   4、年报表");
			System.out.println("\t\t\t\t\t\t\t\t   5、订单报表");
			System.out.println("\t\t\t\t\t\t\t\t   0、退出");	
			System.out.println("\t\t\t\t\t\t\t\t☆                            ☆");
			System.out.println();
			System.out.print("\t\t\t\t❤请输入操作数字：");
			
			choose = scanner.nextInt();
			switch(choose){
			case 1:
				coumtDayOrders();
				break;
			case 2:
				coumtWeekOrders();
				break;
			case 3:
				coumtMonthOrders();
				break;
			case 4:
				coumtYearOrders();
				break;
			case 5:
				countOrders();
				break;
			case 0:
				break;
			}
		}
	}

}
